package com.card.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.card.R;
import com.card.db.entity.User;
import com.card.db.service.UserService;
import com.card.utils.StringUtils;

public class RegisterActivity extends AppCompatActivity {
    private String telephone;
    private String password;
    private String password4Check;

    EditText schoolCardEdit;
    EditText passwordEdit;
    EditText password4CheckEdit;

    private UserService userService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        userService = new UserService(this);
    }

    /**
     * 跳转回初始菜单界面
     *
     * @param view
     */
    public void registerToMain(View view) {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
    }

    /**
     * 注册
     *
     * @param view
     */
    public void register(View view) {
        initData();
        //正则表达式
        String passwordPattern = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,20}$";

        String telephonePattern = "^1(?:3\\d|4[4-9]|5[0-35-9]|6[67]|7[013-8]|8\\d|9\\d)\\d{8}$";

        if (!telephone.matches(telephonePattern)){
            Toast.makeText(this,"手机格式错误",Toast.LENGTH_LONG).show();
            return;
        }

        //校验密码是否符合条件（6 - 20 位 而且 包含数据和英文）
        if (!password.matches(passwordPattern)) {
            //清空密码
            passwordEdit.setText("");
            password4CheckEdit.setText("");
            Toast.makeText(this, "密码至少包含 数字和英文，长度6-20", Toast.LENGTH_LONG).show();
            return;
        }
        //先校验两次密码是否正确
        if (StringUtils.isEmpty(password, password4Check)) {
            //都不为看空
            if (!password.equals(password4Check)) {
                passwordEdit.setText("");
                password4CheckEdit.setText("");
                Toast.makeText(this, "两次密码必须相同", Toast.LENGTH_LONG).show();
                return;
            }
        }
        //判断是否注册成功
        Boolean success = userService.register(telephone, password);
        if (success) {
            User user = userService.login(telephone, password);
            Toast.makeText(this, "注册成功", Toast.LENGTH_LONG).show();
            //存放数据到bundle
            Intent intent = new Intent(this, FunctionActivity.class);
            Bundle bundle = new Bundle();
            bundle.putInt("id", user.getId());
            bundle.putString("telephone", telephone);
            intent.putExtras(bundle);
            startActivity(intent);
        } else {
            //注册失败
            Toast.makeText(this, "该账户已经存在", Toast.LENGTH_LONG).show();
        }


    }

    /**
     * 初始化数据
     */
    public void initData() {
        schoolCardEdit = findViewById(R.id.schoolCardNo);
        passwordEdit = findViewById(R.id.password);
        password4CheckEdit = findViewById(R.id.password_check);

        telephone = schoolCardEdit.getText().toString();
        password = passwordEdit.getText().toString();
        password4Check = password4CheckEdit.getText().toString();
    }


}
